import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_utils/flutter_utils.dart';
import 'package:flutter_widget/src/config/color_define.dart';
import 'dart:io';

///格式化 文本输入框
class TextFieldFormatWidget extends StatelessWidget {
  final TextEditingController? controller;
  final TextInputType? inputType; //输入键盘模式
  final int? maxLines; //最大行数
  final int? maxTextLength; //最多文本长度
  final Widget? outsideIcon; //外部左侧小图标组件
  final Widget? leftIcon; //内部左侧小图标组件
  final Widget? rightIcon; //内部右侧小图标组件
  final Color? cursorColor; //光标颜色
  final double textFontSize; //输入文本大小
  final Color? textColor; //输入文本颜色
  final FontWeight textFontWeight; //输入文本weight
  final String? placeHolderText; //提示文本
  final double? placeHolderSize; //提示文本大小
  final Color placeHolderColor; //提示文本颜色
  final FontWeight? placeHolderFontWeight; //提示文本weight
  final int placeHolderMaxLines; //提示文本最大行数
  final String? counterText; //数量文本，右下角eg: 0/20，不要显示则传入空字符串
  final Widget? counter;
  final InputBorder border; //默认边框, 默认没边框
  final InputBorder? enabledBorder; //使能边框
  final InputBorder? focusedBorder; //获取焦点时的边框
  final bool hasUnderline; //方式有下划线
  final Color underLineDefaultColor; //下划线颜色
  final Color? underLineHighlightColor; //下划线高亮颜色
  final TextInputAction? textInputAction; //键盘事件
  final Brightness keyboardAppearance; //键盘颜色
  final ValueChanged<String>? onChanged; //文字改变
  final ValueChanged<String>? onSubmitted; //点击确认
  final VoidCallback? onEditingComplete; //点击编辑的动作回调
  final double textFontHeight;
  final TextAlign? textAlign;
  final InputCounterWidgetBuilder? buildCounter;
  final FocusNode? focusNode; //焦点
  final bool? autofocus; //自动对焦
  final bool enabled; //用户输入使能
  final TextAlignVertical? textAlignVertical;

  //https://www.cnblogs.com/yongfengnice/p/14143805.html isCollapsed: true,//重点，相当于高度包裹的意思，必须设置为true，不然有默认奇妙的最小高度
  final InputDecoration? decoration;
  final TextStyle? style;
  final double? cursorHeight;
  final ScrollController? scrollController;
  final ScrollPhysics? scrollPhysics;
  final EdgeInsets scrollPadding;
  final List<TextInputFormatter>? inputFormatters;

  const TextFieldFormatWidget({
    Key? key,
    this.controller,
    this.inputType,
    this.maxLines,
    this.maxTextLength,
    this.outsideIcon,
    this.leftIcon,
    this.rightIcon,
    this.cursorColor,
    this.textFontSize = 15,
    this.textColor,
    this.textFontWeight = FontWeight.normal,
    this.placeHolderText,
    this.placeHolderColor = const Color(0xFFAAAAAA),
    this.placeHolderSize,
    this.placeHolderFontWeight,
    this.placeHolderMaxLines = 1,
    this.counterText,
    this.counter,
    this.border = InputBorder.none,
    this.focusedBorder,
    this.enabledBorder,
    this.hasUnderline = false,
    this.underLineDefaultColor = const Color(0xFFF5F5F5),
    this.underLineHighlightColor,
    this.textInputAction,
    this.keyboardAppearance = Brightness.light,
    this.onChanged,
    this.onEditingComplete,
    this.onSubmitted,
    this.textAlign,
    this.textFontHeight = 1,
    this.buildCounter,
    this.focusNode,
    this.enabled = true,
    this.autofocus = false,
    this.textAlignVertical,
    this.decoration,
    this.style,
    this.cursorHeight,
    this.scrollController,
    this.scrollPhysics,
    this.scrollPadding = const EdgeInsets.all(20.0),
    this.inputFormatters,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Platform.isAndroid
        ? Theme(
            data: ThemeData(
              platform: TargetPlatform.android,
            ),
            child: createTextField(),
          )
        : createTextField();
  }

  Widget createTextField() {
    return TextField(
      autofocus: autofocus ?? false,
      controller: controller,
      scrollPadding: scrollPadding,
      scrollController: scrollController,
      scrollPhysics: scrollPhysics,
      focusNode: focusNode,
      textInputAction: textInputAction,
      keyboardAppearance: keyboardAppearance,
      keyboardType: inputType,
      maxLines: maxLines,
      maxLength: maxTextLength,
      enabled: enabled,
      cursorColor: cursorColor ?? FWColorPicker.colorFFDAB27E,
      cursorHeight: cursorHeight,
      textAlign: textAlign ?? TextAlign.left,
      style: style ??
          (textColor != null
              ? TextStyle(
                  fontSize: textFontSize,
                  color: textColor,
                  fontWeight: textFontWeight,
                  height: textFontHeight,
                  textBaseline: TextBaseline.ideographic,
                )
              : null),
      onChanged: onChanged,
      onEditingComplete: onEditingComplete,
      onSubmitted: onSubmitted,
      buildCounter: buildCounter,
      textAlignVertical: textAlignVertical,
      inputFormatters: inputFormatters ??
          [
            InputFormatter(),
          ],
      decoration: decoration ??
          InputDecoration(
            icon: outsideIcon,
            prefixIcon: leftIcon,
            suffixIcon: rightIcon,
            // suffix: rightIcon,
            // prefix: leftIcon,
            counterText: counterText,
            counter: counter,
            hintText: placeHolderText,
            hintMaxLines: placeHolderMaxLines,
            hintStyle: TextStyle(
              fontSize: placeHolderSize ?? textFontSize,
              color: placeHolderColor,
              fontWeight: placeHolderFontWeight ?? textFontWeight,
              textBaseline: TextBaseline.ideographic,
            ),
            // 未获得焦点下划线
            border: border,
            enabledBorder: hasUnderline == true
                ? UnderlineInputBorder(
                    borderSide: BorderSide(
                      color: underLineDefaultColor,
                    ),
                  )
                : enabledBorder,
            //获得焦点下划线
            focusedBorder: hasUnderline == true
                ? UnderlineInputBorder(
                    borderSide: BorderSide(
                      color: underLineHighlightColor ?? underLineDefaultColor,
                    ),
                  )
                : focusedBorder,
          ),
    );
  }
}

class InputFormatter extends TextInputFormatter {
  final String? regExp;

  InputFormatter({this.regExp});

  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    if (newValue.text.isNotEmpty && regExp?.isNotEmpty == true) {
      if (RegExp(regExp!).firstMatch(newValue.text) != null) {
        return newValue;
      }
      return oldValue;
    }
    if (newValue.text.isNotEmpty) {
      var displayText = TextUtil.displayEmojiText(newValue.text);
      var value = newValue.copyWith(
        text: displayText,
        selection: newValue.selection,
        composing: newValue.composing,
      );
      return value;
    }
    return newValue;
  }
}
